package com.yuwei.play.fragment

import android.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.tencent.mmkv.MMKV
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.play.R
import com.yuwei.play.adapter.CommentAdapter
import com.yuwei.play.adapter.CommentTwoAdapter
import com.yuwei.play.databinding.FragmentCommentsBinding
import com.yuwei.play.entity.CommentEntity
import com.yuwei.play.entity.ReplyBody
import com.yuwei.play.entity.SendCommentBody
import com.yuwei.play.mvi.PlayIntent
import com.yuwei.play.mvi.PlayViewModel
import java.text.SimpleDateFormat


class CommentsFragment : BaseMviFragment<FragmentCommentsBinding,PlayViewModel>() {
    private val commentAdapter = CommentAdapter()
    private val commentTwoAdapter = CommentTwoAdapter()
    private var mPosition = 0
    private var check = false
    private var itemId = SPUtils.getInstance().getString("itemId")
    override fun successData(it: UiState.onSuccess<*>) {
        when (it.type) {
            "getVideoComment" -> {
                val data = it.data as CommentEntity
                commentAdapter.data.clear()
                commentAdapter.data.addAll(data.reversed())
                commentAdapter.notifyDataSetChanged()
            }
            "sendComment" -> {
                vm.sendIntent(PlayIntent.getVideoComment(0,if (itemId !=null)itemId else "7046705001120399908"))

            }
            "reComment"-> {
                vm.sendIntent(PlayIntent.getVideoComment(0,if (itemId !=null)itemId else "7046705001120399908"))
                binding.sendCommentEt.hint = "来谈谈自己的看法吧~"
                check = false
            }
            "commentDelete" ->{
                vm.sendIntent(PlayIntent.getVideoComment(0,if (itemId !=null)itemId else "7046705001120399908"))
                ToastUtils.showLong("删除成功")
            }
            "replyDelete" ->{
                vm.sendIntent(PlayIntent.getVideoComment(0,if (itemId !=null)itemId else "7046705001120399908"))
                ToastUtils.showLong("删除成功")
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        binding.commentRv.adapter = commentAdapter
        binding.commentRv.layoutManager = LinearLayoutManager(requireContext())
        binding.commentRv.setItemViewCacheSize(20)
        vm.sendIntent(PlayIntent.getVideoComment(0,if (itemId !=null)itemId else "7046176703973949964"))

        commentAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            commentAdapter.sendPosition(mPosition)
            check = true
            binding.sendCommentEt.hint = "回复${commentAdapter.data[mPosition].nickname}"

        }
        commentAdapter.setOnItemLongClickListener { adapter, view, position ->
            DeteleComment(position)
            true
        }

        binding.sendCommentBtn.setOnClickListener {
            val string = MMKV.defaultMMKV().decodeString("phone")
            if (check){
                val replyBody = ReplyBody(
                    agreenum = 200,
                    commentid = commentAdapter.data[mPosition].id,
                    content = binding.sendCommentEt.text.toString(),
                    createtime =  "${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())}",
                    id = 0,
                    nickname = string!!,
                    status = 0,
                    userid = 18,
                    userlogo = "",
                )
                vm.sendIntent(PlayIntent.replyComment(replyBody))
                binding.sendCommentEt.text.clear()

            }else{

                val sendCommentBody = SendCommentBody(
                    itemid = if (itemId!=null)itemId else "7046705001120399908",
                    content = binding.sendCommentEt.text.toString(),
                    userid = 18,
                    nickname = string!!,
                    userlogo = "",
                    createtime = "${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())}",
                    replyList = arrayListOf()
                )
                vm.sendIntent(PlayIntent.sendComment(sendCommentBody))
                binding.sendCommentEt.text.clear()
            }

        }
        commentTwoAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(context)
                .setTitle("删除提醒")
                .setMessage("确定要删除${commentTwoAdapter.data[position].nickname}的评论吗？")
                .setPositiveButton("确定"){dialog,_ ->
                    vm.sendIntent(PlayIntent.commentDelete(commentTwoAdapter.data[position].id))
                    dialog.dismiss()
                }
                .setNegativeButton("取消"){item,_ ->
                    item.dismiss()
                }
                .show()
            true
        }



    }
    fun DeteleComment(index:Int){
        AlertDialog.Builder(requireContext())
            .setTitle("删除提醒")
            .setMessage("确定要删除${commentAdapter.data[index].nickname}的评论吗？")
            .setPositiveButton("确定"){dialog,_ ->
                vm.sendIntent(PlayIntent.commentDelete(commentAdapter.data[index].id))
                dialog.dismiss()
            }
            .setNegativeButton("取消"){item,_ ->
                item.dismiss()
            }
            .show()
    }
    override fun initData() {

    }

}